package com.chefu.controller;

import com.chefu.common.pojo.PageResult;
import com.chefu.serve.pojo.FCategory;
import com.chefu.serve.pojo.SCategory;
import com.chefu.serve.pojo.Serve;
import com.chefu.service.ServeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ServeController {
    @Autowired
    private ServeService serveService;
    //添加服务一级分类
    @PostMapping("/serve/category/add")
    public ResponseEntity<Void> add(@RequestBody FCategory fCategory){
        serveService.add(fCategory);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //查询服务一级分类分页
    @GetMapping("/serve/category/selectPage")
    public ResponseEntity<PageResult<FCategory>> selectPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "15") Integer rows,
            @RequestParam(value = "id",required = false) Long id,
            @RequestParam(value = "name",required = false) String name){
        PageResult<FCategory> result =  serveService.selectPage(page,rows,name,id);
        return ResponseEntity.ok(result);
    }
//
//    //查询服务一级分类
//    @GetMapping("/serve/category/search")
//    public ResponseEntity<FCategory> search(@RequestParam(value = "id",required = false) Long id,
//                                            @RequestParam(value = "name",required = false) String name){
//        FCategory fCategory = serveService.search(id,name);
//        return ResponseEntity.ok(fCategory);
//    }

    //修改服务分类
    @PutMapping("/serve/category/change")
    public ResponseEntity<Void> change(@RequestParam(value = "id",required = false) Long id,
                                       @RequestParam(value = "name",required = false)String name){
        serveService.change(id,name);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //删除服务分类
    @DeleteMapping("/serve/category/delete")
    public ResponseEntity<Void> delete(@RequestParam("id") Long id){
        serveService.delete(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //新增服务二级分类
    @PostMapping("/serve/category/second/add")
    public ResponseEntity<Void> addS(@RequestBody SCategory sCategory){
        serveService.addS(sCategory);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //修改服务二级分类
    @PutMapping("/serve/category/second/change")
    public ResponseEntity<Void> changeS(@RequestBody SCategory sCategory){
        serveService.changeS(sCategory);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //删除服务子分类
    @DeleteMapping("/serve/category/second/delete")
    public ResponseEntity<Void> deleteS(@RequestParam("sonId") Long sonId,@RequestParam("parentId") Long parentId ){
        serveService.deleteS(sonId,parentId);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //查询服务二级分类分页
    @GetMapping("/serve/category/second/search")
    public ResponseEntity<PageResult<FCategory>> searchS(@RequestParam(value = "sonId",required = false) Long sonId,
                                             @RequestParam(value = "sonName",required = false) String sonName,
                                             @RequestParam(value = "page",defaultValue = "1") Integer page,
                                             @RequestParam(value = "rows",defaultValue = "15") Integer rows
                                             ){
        PageResult<FCategory> pageResult = serveService.searchS(sonId,sonName,page,rows);
        return ResponseEntity.ok(pageResult);
    }

    //查询服务一级分类名称集合
    @GetMapping("/serve/category/selectName")
    public ResponseEntity<List<String>> selectName(){
        List<String> nameList = serveService.selectName();
        return ResponseEntity.ok(nameList);
    }

    //查询服务二级分类名称集合
    @GetMapping("/serve/category/second/selectName")
    public ResponseEntity<List<String>> selectSName(@RequestParam("parentName") String parentName){
        List<String> list = serveService.selectSName(parentName);
        return ResponseEntity.ok(list);
    }

    //查询服务目录分页
    @GetMapping("/serve/selectPage")
    public ResponseEntity<PageResult<Serve>> selectPageOfServe(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "15") Integer rows,
            @RequestParam(value = "brandName",required = false) String brandName,
            @RequestParam(value = "sonId",required = false) Long sonId,
            @RequestParam(value = "sonName",required = false) String sonName,
            @RequestParam(value = "serveName",required = false) String serveName
    ){
        PageResult<Serve> pageResult = serveService.selectPageOfServe(page,rows,brandName,sonId,sonName,serveName);
        return ResponseEntity.ok(pageResult);
    }

    //查看服务详情
    @GetMapping("/serve/select/of/checker")
    public ResponseEntity<Serve> selectOfChecker(@RequestParam("sonId") Long sonId){
        Serve serve = serveService.selectOfChecker(sonId);
        return ResponseEntity.ok(serve);
    }

    //运营人员修改服务上下架
    @PutMapping("/serve/update/of/checker")
    public ResponseEntity<Void> updateOfChecker(@RequestParam("sonId") Long sonId,
                                                @RequestParam("saleable") Integer saleable){
        serveService.updateOfChecker(sonId,saleable);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //服务商添加服务
    @PostMapping("/serve/add")
    public ResponseEntity<Void> addByShop(@RequestBody Serve serve){
        serveService.addByShop(serve);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
